// pages/my/userinfo.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		sex: 1,//性别 1.男 2.女
		birthday: '',
		birthdayStr: '',
		userInfo: {},
		is_avatar: false, //是否修改了头像
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this;
		app.login(function (userInfo) {
			if (userInfo.data.birthday != null) {
				var birthday = userInfo.data.birthday.split("-");
			} else {
				var birthday = false;
			}
			that.setData({
				userInfo: userInfo.data,
				sex: userInfo.data.gender,
				birthday: birthday,
				birthdayStr: userInfo.data.birthday,
			})
		})
	},

	radioChange: function (e) {
		var value = e.detail.value;
		this.setData({
			sex: value
		});
		console.log(e);
	},
	bindDateChange: function (e) {

		this.setData({
			birthday: e.detail.value.split("-"),
			birthdayStr: e.detail.value
		})
		console.log(this.data.birthday);
	},
	submit: function (e) {
		console.log(e);
		let that = this;
		console.log(e.detail.value.qianming.length);
		if (e.detail.value.qianming.length > 20) {
			console.log(e.detail.value.qianming.length);
			wx.showToast({
				title: '签名不得超过20个字',
				icon:'none'
			})
			return false;
		} 
	
		wx.showLoading()
		app.connect('account/editAccount', {
			uid: that.data.userInfo.uid,
			openid: that.data.userInfo.openId,
			qianming: e.detail.value.qianming,
			sex: that.data.sex,
			nick: e.detail.value.nick,
			birthday: e.detail.value.birthday
		}, function (e) {

			wx.showToast({
				title: '修改成功',
			})
		
			app.login();
			setTimeout(function () {
				wx.reLaunch({
					url: 'index',
				})
			}, 1000);
		});
	},
	chooseImg: function () {
		let that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths[0];
				var userInfo = that.data.userInfo;
                    userInfo.avatarUrl = tempFilePaths;
                   
				// that.setData({
				// 	userInfo: userInfo,
				// 	is_avatar: true
				// });
                    wx.navigateTo({
                         url: `cutting?src=${tempFilePaths}`
                    })
			}
		})
	}
})